package com.echo.im.domain.auth;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.fun.SaParamFunction;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.echo.im.common.contant.Constants;
import com.echo.im.common.result.ResultEnum;
import com.echo.im.common.exception.GlobalException;
import com.echo.im.infrastructure.session.SessionContext;
import com.echo.im.infrastructure.session.UserSession;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/**
 *
 * @author echo
 * @date 2024-08-16
 */
@Slf4j
public class AuthHandler implements SaParamFunction<Object> {

    @Override
    public void run(Object o) {
        //从 http 请求头中取出 token
        try {
            StpUtil.checkLogin();
        }catch (NotLoginException e){
            if ("-1".equals(e.getType())){
                throw new GlobalException(ResultEnum.NO_LOGIN);
            }else {
                throw new GlobalException(ResultEnum.INVALID_TOKEN);
            }

        }

        UserSession session = BeanUtil.copyProperties(StpUtil.getTokenSession().get(Constants.TOKEN_SESSION_NAME), UserSession.class);
        SessionContext.setSession(session);
        // 存放session
    }
}
